﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Neovolve.Extensibility.VisualStudio.ReflectorLink
{
    /// <summary>
    /// The <see cref="LaunchActionCollection"/>
    /// class is used to hold a collection of <see cref="LaunchAction"/> objects.
    /// </summary>
    /// <remarks>
    /// The collection ensures that only unique launch actions are held.
    /// </remarks>
    internal class LaunchActionCollection : ICollection<LaunchAction>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LaunchActionCollection"/> class.
        /// </summary>
        public LaunchActionCollection()
        {
            Actions = new Dictionary<String, LaunchAction>();
        }

        /// <summary>
        /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
        public void Add(LaunchAction item)
        {
            if (item == null)
            {
                return;
            }

            // Check if the action already exists
            if (Contains(item))
            {
                return;
            }

            Actions.Add(item.ToString(), item);
        }

        /// <summary>
        /// Adds the range.
        /// </summary>
        /// <param name="actions">The actions.</param>
        public void AddRange(IEnumerable<LaunchAction> actions)
        {
            if (actions == null)
            {
                return;
            }

            // Loop through each new action
            foreach (LaunchAction action in actions)
            {
                // Add the action
                Add(action);
            }
        }

        /// <summary>
        /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only. </exception>
        public void Clear()
        {
            Actions.Clear();
        }

        /// <summary>
        /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
        /// </summary>
        /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
        /// <returns>
        /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
        /// </returns>
        public Boolean Contains(LaunchAction item)
        {
            return Actions.ContainsValue(item);
        }

        /// <summary>
        /// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
        /// </summary>
        /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
        /// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// 	<paramref name="array"/> is null.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        /// 	<paramref name="arrayIndex"/> is less than 0.</exception>
        /// <exception cref="T:System.ArgumentException">
        /// 	<paramref name="array"/> is multidimensional.-or-<paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.-or-The number of elements in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.-or-Type <paramref name="LaunchAction"/> cannot be cast automatically to the type of the destination <paramref name="array"/>.</exception>
        public void CopyTo(LaunchAction[] array, int arrayIndex)
        {
            Actions.Values.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<LaunchAction> GetEnumerator()
        {
            return Actions.Values.GetEnumerator();
        }

        /// <summary>
        /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
        /// <returns>
        /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </returns>
        /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
        public Boolean Remove(LaunchAction item)
        {
            return Actions.Remove(item.ToString());
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable<LaunchAction>)this).GetEnumerator();
        }

        /// <summary>
        /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
        /// </summary>
        /// <value></value>
        /// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</returns>
        public Int32 Count
        {
            get
            {
                return Actions.Count;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.</returns>
        public Boolean IsReadOnly
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Gets or sets the actions.
        /// </summary>
        /// <value>The actions.</value>
        private Dictionary<String, LaunchAction> Actions
        {
            get;
            set;
        }
    }
}